package tq.com.tqstudent.views;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lib.common.utils.AppUtil;

import java.text.NumberFormat;
import java.util.ArrayList;

import tq.com.tqstudent.R;

/**
 * @Author rames
 * @date 2019/3/28 15:41
 */
public class CommentHeader extends LinearLayout {
    private TextView scoreTV;
    private TextView countTV;
    private ImageView starIV1, starIV2, starIV3, starIV4, starIV5;
    private TextView scoreTV1, scoreTV2, scoreTV3, scoreTV4, scoreTV5;
    private View scoreView1, scoreView2, scoreView3, scoreView4, scoreView5;

    public CommentHeader(Context context) {
        super(context);
        init();
    }

    public CommentHeader(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.item_head_comment, this);
        scoreTV = findViewById(R.id.score_tv);
        countTV = findViewById(R.id.count_tv);
        starIV1 = findViewById(R.id.star_iv1);
        starIV2 = findViewById(R.id.star_iv2);
        starIV3 = findViewById(R.id.star_iv3);
        starIV4 = findViewById(R.id.star_iv4);
        starIV5 = findViewById(R.id.star_iv5);

        scoreTV1 = findViewById(R.id.one_tv);
        scoreTV2 = findViewById(R.id.two_tv);
        scoreTV3 = findViewById(R.id.three_tv);
        scoreTV4 = findViewById(R.id.four_tv);
        scoreTV5 = findViewById(R.id.five_tv);

        scoreView1 = findViewById(R.id.one_view);
        scoreView2 = findViewById(R.id.two_view);
        scoreView3 = findViewById(R.id.three_view);
        scoreView4 = findViewById(R.id.four_view);
        scoreView5 = findViewById(R.id.five_view);
    }

    /**
     * @param scores 5星--->1星
     * @param count  评论的人数
     * @param score  10分制
     */
    public void setValue(ArrayList<Integer> scores, String count, int score) {
        NumberFormat nf = NumberFormat.getInstance();
        scoreTV.setText(String.valueOf(nf.format(((float) (score))/10)));
        countTV.setText(count + "人评价");
        setStarLevel(score / 20);

        scoreTV5.setText(String.valueOf(scores.get(0))+"%");
        scoreTV4.setText(String.valueOf(scores.get(1))+"%");
        scoreTV3.setText(String.valueOf(scores.get(2))+"%");
        scoreTV2.setText(String.valueOf(scores.get(3))+"%");
        scoreTV1.setText(String.valueOf(scores.get(4))+"%");

        setProgress(scoreView5, scores.get(0));
        setProgress(scoreView4, scores.get(1));
        setProgress(scoreView3, scores.get(2));
        setProgress(scoreView2, scores.get(3));
        setProgress(scoreView1, scores.get(4));
    }

    private void setStarLevel(int index) {
        starIV1.setImageResource(R.mipmap.icon_star_checked);
        starIV2.setImageResource(R.mipmap.icon_star_checked);
        starIV3.setImageResource(R.mipmap.icon_star_checked);
        starIV4.setImageResource(R.mipmap.icon_star_checked);
        starIV5.setImageResource(R.mipmap.icon_star_checked);

        if (index == 0) {
            starIV1.setImageResource(R.mipmap.icon_star_normal);
            starIV2.setImageResource(R.mipmap.icon_star_normal);
            starIV3.setImageResource(R.mipmap.icon_star_normal);
            starIV4.setImageResource(R.mipmap.icon_star_normal);
            starIV5.setImageResource(R.mipmap.icon_star_normal);
        } else if (index == 1) {
            starIV2.setImageResource(R.mipmap.icon_star_normal);
            starIV3.setImageResource(R.mipmap.icon_star_normal);
            starIV4.setImageResource(R.mipmap.icon_star_normal);
            starIV5.setImageResource(R.mipmap.icon_star_normal);
        } else if (index == 2) {
            starIV3.setImageResource(R.mipmap.icon_star_normal);
            starIV4.setImageResource(R.mipmap.icon_star_normal);
            starIV5.setImageResource(R.mipmap.icon_star_normal);
        } else if (index == 3) {
            starIV4.setImageResource(R.mipmap.icon_star_normal);
            starIV5.setImageResource(R.mipmap.icon_star_normal);
        } else if (index == 4) {
            starIV5.setImageResource(R.mipmap.icon_star_normal);
        }
    }

    private void setProgress(View view, int score){
        if(score == 0) score = 1;
        float s = score/100f;
        LinearLayout.LayoutParams params = (LayoutParams) view.getLayoutParams();
        params.width = AppUtil.dp2px(getContext(), 118*s);
        view.setLayoutParams(params);
    }
}
